package com.haiqi.modules.map.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.DataGridBean;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.CyxShip;
import com.haiqi.common.persistence.entity.ShipPositionInfo;
import com.haiqi.common.persistence.entity.ShipServer;
import com.haiqi.common.utils.PageUtils;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.modules.map.service.ShipServerService;
import com.haiqi.modules.ship.service.AisShipDynaService;
import com.haiqi.modules.ship.service.ShipPositionInfoService;
import com.haiqi.modules.ship.service.ShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuxiayan on 2017/4/20.
 */
@RequestMapping("/manage/map/server")
@Controller
@SafeAuthority(menuCode = "monitorLocation,companyMonitorLocation")
public class ShipServerController extends BaseController {
    @Autowired
    private ShipServerService shipServerService;

    @Autowired
    private ShipPositionInfoService shipPositionInfoService;

    @Autowired
    private ShipService shipService;

    @Autowired
    private AisShipDynaService aisShipDynaService;

    private String menuCode = "";
    @Value("${geo.server}")
    private String geoServer;


    @RequestMapping("/publishShip")
    public String shipList(ShipPositionInfo shipPositionInfo, PageBean pageBean, HttpSession session, Model model,String key) {
        CyxShip cyxShip=new CyxShip();
        ShipServer shipServer=new ShipServer();
        ResponseBean.UserBean user = getUser(session);
        cyxShip.setCompanyId(user.getOfficeid());
        cyxShip.setShipName(key);
        cyxShip.setMmsi(key);
        shipServer.setKeyword(key);
        shipPositionInfo.setShipServer(shipServer);
        shipPositionInfo.setCyxShip(cyxShip);
        menuCode = "serviceShip";
        generateMenuCode(session, menuCode);
        PageInfo<ShipPositionInfo> pageInfo = shipPositionInfoService.selectByServer(shipPositionInfo, pageBean);
        model.addAttribute("param", shipPositionInfo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("key", key);
        return "/modules/service/publish-boat";
    }

    @RequestMapping("/searchShip")
    public String searchShip(ShipPositionInfo shipPositionInfo, PageBean pageBean, HttpSession session, Model model,String key) {
        menuCode = "serviceshipSearch";
        CyxShip cyxShip=new CyxShip();
        ShipServer shipServer=new ShipServer();
        cyxShip.setShipName(key);
        cyxShip.setMmsi(key);
        shipServer.setKeyword(key);
        shipServer.setStatus("1");
        shipPositionInfo.setShipServer(shipServer);
        shipPositionInfo.setCyxShip(cyxShip);
        shipServer.setKeyword(key);
        generateMenuCode(session, menuCode);
        PageInfo<ShipPositionInfo> pageInfo = shipPositionInfoService.selectByServer(shipPositionInfo, pageBean);
        model.addAttribute("param", shipServer);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("key", key);
        return "/modules/service/search-boat";
    }

    @RequestMapping("/saveserver")
    @ResponseBody
    public Map saveserver(ShipServer shipServer){
        Map map = new HashMap();
        String keyword = shipServer.getKeyword();
        keyword=keyword.replace('，',',');
        String[] arr = keyword.split(",");
        if(arr.length > 3){
            map.put("result", false);
            map.put("keyword",false);
            return map;
        }else {
            for (String s : arr) {
                if (s.length() > 5) {
                    map.put("result", false);
                    map.put("keyword", false);
                    return map;
                }
            }
        }
        boolean result = shipServerService.saveserver(shipServer);
        map.put("ssid",shipServer.getSsid());
        map.put("result",result);
        return map;
    }



    @ResponseBody
    @RequestMapping("/ajax/shipServer/list")
    public List<ShipServer> getShipServer(ShipServer shipServer){
        List<ShipServer> dataList = shipServerService.getShipServerList(shipServer);
        return dataList;
    }


    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(ShipServer shipServer){
        boolean  success = shipServerService.delete(shipServer);
        Map map = new HashMap();
        map.put("result",success);
        return map;
    }

    @ResponseBody
    @RequestMapping("/ajax/data/page/list")
    public DataGridBean<ShipServer> getPageDataList(ShipPositionInfo shipPositionInfo, PageBean page,String key){
        CyxShip cyxShip=new CyxShip();
        ShipServer shipServer=new ShipServer();
        cyxShip.setMmsi(key);
        cyxShip.setShipName(key);
        shipServer.setKeyword(key);
        shipPositionInfo.setCyxShip(cyxShip);
        shipPositionInfo.setShipServer(shipServer);
        PageInfo<ShipPositionInfo> pageInfo = shipPositionInfoService.selectShipServerCheck(shipPositionInfo,page);
        DataGridBean<ShipServer> mobileShipDataGridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo, mobileShipDataGridBean);
        return mobileShipDataGridBean;
    }

    @ResponseBody
    @RequestMapping("/ajax/shipServer/page/list")
    public DataGridBean<ShipServer> getShipServerDataList(HttpSession session,ShipServer shipServer, PageBean page){
        ResponseBean.UserBean user = getUser(session);
        CyxShip cyxShip=new CyxShip();
        ShipPositionInfo shipPositionInfo=new ShipPositionInfo();
        cyxShip.setCompanyId(user.getOfficeid());
        shipPositionInfo.setCyxShip(cyxShip);
        PageUtils.generatePage(page);
        List<ShipServer> dataList = shipServerService.selectShipServer(shipServer);
        PageInfo<ShipServer> pageInfo = new PageInfo<>(dataList);
        DataGridBean<ShipServer> mobileShipDataGridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo, mobileShipDataGridBean);
        return mobileShipDataGridBean;
    }

    @RequestMapping("/dropServer")
    @ResponseBody
    public Map dropAlert(String ssid){
        Map<String,Boolean> response = new HashMap<>();
        boolean success = shipServerService.dropServer(ssid);
        response.put("success",success);
        return  response;
    }

    @RequestMapping("/detail")
    @ResponseBody
    public ShipServer detail(String ssid){
        ShipServer shipServer = shipServerService.getById(ssid);
        return  shipServer;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map update(ShipServer shipServer){
        Map<String,Boolean> response = new HashMap<>();
        boolean success = shipServerService.saveserver(shipServer);
        response.put("success",success);
        return  response;
    }


}
